import requests
import os,time,json,re
from urllib import request
from urllib.request import urlretrieve
from urllib.parse import urlencode


# 获取网页信息
def getPage(m):
	data = {
		'tn':'resultjson_com',
		'ipn':'rj',
		'queryWord':"街拍",
		'word':"街拍",
		'pn':m*30
	}
	head = {
		'Accept':'text/plain, */*; q=0.01',
		'Accept-Encoding':'gzip, deflate, br',
		'Host':'image.baidu.com',
		'Referer':'https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=index&fr=&hs=0&xthttps=111111&sf=1&fmq=&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=%E8%A1%97%E6%8B%8D&oq=%E8%A1%97%E6%8B%8D&rsp=-1',
		'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36',
	}
	url = "http://image.baidu.com/search/acjson?"+urlencode(data)
	html = requests.get(url)
	try:
		if html.status_code == 200:
			con = html.content.decode("utf-8","ignore")
			return json.loads(con)
	except requests.ConnectionError as err:
		print(err)
		return None
	except Exception as err:
		print(err)
		return None
# 获取并封装返回图片url及图片标题
def getImage(json):
	data = json.get('data')
	if data:
		for item in data:
			if item:
				yield{
					'img_url':item.get("thumbURL"),
					'title':item.get("fromPageTitleEnc")
				}
	else:
		print("未能解析信息")

# 保存图片
def savepic(data,m):
	img_url = data.get("img_url")
	title = data.get("title")
	t = re.sub(r",|\"|:|\.|！|\?|\s+","",title)
	# 建立文件夹
	if not os.path.exists("./pic"):
		os.mkdir("pic")
	path = os.path.join("./pic/",t)	
	if not os.path.exists(path):
		os.mkdir(path)
	# 为urlretrieve 伪装头信息，否则会拒绝访问403
	opener=request.build_opener()	
	opener.addheaders = [
		('Accept','application/json, text/javascript, */*; q=0.01'),
		('Cache-Control','max-age=0'),
		('Connection','keep-alive'),
		('Referer','https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=index&fr=&hs=0&xthttps=111111&sf=1&fmq=&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=%E8%A1%97%E6%8B%8D&oq=%E8%A1%97%E6%8B%8D&rsp=-1'),
		('Upgrade-Insecure-Requests','1'),
		('User-Agent','Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2969.0 Safari/537.36'),
	]
	request.install_opener(opener)
	#存储图片
	urlretrieve(img_url,path+"/"+str(m)+".jpg")

# 主函数，控制程序流程
def main(offset):
	json = getPage(offset)
	# print(str)
	m=1+offset*30
	
	for i in getImage(json):
		# print(i)
		savepic(i,m)
		m +=1

if __name__ == '__main__':
	# main(4)
	# 爬取前六页，共180张图片
	for i in range(6):
		main(offset=i*20)
		time.sleep(1)


